<?php
	
	#
	# Garlic PHP/0.10
	# Copyleft (C) 2010 By Philip Patrick Butkiewicz (Bagno/Yuuki) and others (see "AUTHORS")
	# Project website: http://code.google.com/p/garlic-php
	#
	
	class garlicStrings {
	
		private $_htmlchars = array("&nbsp", "&lt;", "&gt;", "&amp;", "&laquo;", "&raquo");
	
		# Get rid of the crap!
		public function strClear((string)$str) {
			if (get_magic_quotes_gpc()) {
				$str = stripslashes($str);
			}
			
			# Remove HTML special chars
			$str = str_replace($this->_htmlchars, "", $str);
			$str = preg_replace("#(&\#*\w+)[\x00-\x20]+;#u", "$1;", $str);
			$str = preg_replace("#(&\#x*)([0-9A-F]+);*#iu", "$1$2;", $str);
			$str = html_entity_decode($str, ENT_COMPAT, "UTF-8");
			
			# Remove attributes starting with "on" or "xmlns"
			$str = preg_replace("#(<[^>]+[\x00-\x20\"\'\/])(on|xmlns)[^>]*>#iUu", "$1>", $str);
			
			# Remove "javascript:" and "vbscript:"
			$str = preg_replace("#([a-z]*)[\x00-\x20\/]*=[\x00-\x20\/]*([\'\'\"]*)[\x00-\x20\/]
					*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]
					*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iUu', '$1=$2nojavascript...", $str);
			
        		$str = preg_replace("#([a-z]*)[\x00-\x20\/]*=[\x00-\x20\/]*([\'\'\"]*)[\x00-\x20\/]*v
        				[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p
        				[\x00-\x20]*t[\x00-\x20]*:#iUu', '$1=$2novbscript...", $str);
        				
        		$str = preg_replace("#([a-z]*)[\x00-\x20\/]*=[\x00-\x20\/]*([\'\'\"]*)[\x00-\x20\/]*-moz-binding
        				[\x00-\x20]*:#Uu', '$1=$2nomozbinding...", $str);
        				
        		$str = preg_replace("#([a-z]*)[\x00-\x20\/]*=[\x00-\x20\/]*([\'\'\"]*)[\x00-\x20\/]
        				*data[\x00-\x20]*:#Uu', '$1=$2nodata...", $str);
        				
        		# Remove IE-style crap
        		$str = preg_replace("#(<[^>]+[\x00-\x20\"\'\/])style[^>]*>#iUu", "$1>", $str);
        		
        		# Remove namespaced things
        		$str = preg_replace("#</*\w+:\w[^>]*>#i", "", $str);
        		
        		while ($_str !== $str) {
        			$_str = $str;
        			$str = preg_replace("#</*(applet|meta|xml|blink|link|style|script|embed|object|iframe|
        				frame|frameset|ilayer|layer|bgsound|title|base)[^>]*>#i", "", $str);
        		}
			
			return $str;
		}

		# Escapes string for use with "
		public function strescape((string)$str) {
			private $_rep = array("\"", "\\", "\$");
			private $_mat = array("\\\"", "\\\\", "\\\$");
			return str_replace($this->_rep, $this->_mat, $str);
		}
		
		# Validate email and return array of strings on success, or return false on failure
		public function emailval((string)$email, (int)$type) {
			
			private $_reg;
			
			switch ($type) {
				# Simple
				case 0:
					$this->_reg = "^\S+@\S+$";
				break;
				
				# Simple, with restriction on characters
				case 1:
					$this->_reg = "^[A-Z0-9+_.-]+@[A-Z0-9.-]+\Z";
				break;
				
				# Simple, with all characters
				case 2:
					$this->_reg = "^[\w!#$%&`*+/=?`{|}~^.-\+@[A-Z0-9.-]+$";
				break;
				
				# No leading, trailing or consecutive dots
				case 3:
					$this->_reg = "^[\w!@$%&'*+/=?`{|}~^-]+(?:\.[\w!#$%&'*+/=?`{|}~^-]+)*@[A-Z0-9-]+(?:\.[A-Z0-9-)+)*\Z";
				break;
			}
			
			if (preg_match($this->_reg, $email) {
				return explode("@", $email);
			} else {
				return false;
			}
		}
	}
	
?>
